package com.lagou.edu;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

/**
 * @author 赵乾坤
 * @since 2021-12-05 14:49
 * 配置类 代替applicationContext.xml
 */
@Configuration//配置类
@ComponentScan({"com.lagou.edu"})//包扫描  .xml base-package=
@PropertySource({"classpath:jdbc.properties"})//加载文件 代替  。xml:location
//@Import()//配置类有多个可以关联到一个类里面
public class SpringConfig {

    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")//id
    public DataSource createDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }


}
